/**
 * 迁移到调度
 */

import initServerInstance from '../server/initServerInstance';

export const version = '1.4.1';

if (require.main === module) {
	(async () => {
		const server = await initServerInstance({
			loggerConfig: {
				name: 'upgrade',
				alwaysCreate: true,
			},
		});

		for await (const record of server.app.model.Task.find({
			taskType: { $in: ['vul', 'ssl'] },
		})) {
			// mongo shell
			// db.tasks.find({ taskType: { $nin: ['vul', 'ssl'] } }).forEach(x => db.tasks.update({ _id: x._id }, { $set: { 'alertSettings.enable': { [x.taskType]: !!x.alertSettings.enable } } }))
			if (typeof record.toObject().alertSettings.enable === 'boolean') {
				record.alertSettings.enable = {
					[record.taskType]: Boolean(record.alertSettings.enable),
				};
			}
			if (record.isSpecialTask) {
				record.triggerType = 'special';
			}
			if (record.taskSettings.collect.type === 'increment') {
				record.triggerType = 'scheduleIncrement';
			}
			if (record.status === 'completed') {
				record.result.warnings = record.toObject().result.warnings.map(w => ({
					...w,
					impact: w.detail.impact,
					recommendation: w.detail.recommendation,
					description: w.detail.description,
					reference: w.detail.reference,
				}));
			}

			await record
				.save({ validateBeforeSave: false })
				.catch(error => console.log(error, record));
		}

		process.exit();
	})();
}
